package com.zsrt.scott.py;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import com.zsrt.scott.listener.ContextLoaderListener;

public class ProxyFactory {

	private Object target;
	

	/**
	 * 
	 * 1 维护一个目标对象：给谁去买烟
	 * 
	 * 谁去买烟：代理对象
	 * 
	 * @param studentDao
	 * 
	 */
	public ProxyFactory(/* StudentDao studentDao */ Object target) {
		this.target = target;
		System.out.println("target:" + this.target.getClass());
	}

	/**
	 * 
	 * 给目标对象target创建一个代理对象proxy
	 * 
	 * @return 代理对象
	 * 
	 */
	public Object createProxyInstanceForTarget() {

		// 1 目标对象的类加载器

		ClassLoader loader = target.getClass().getClassLoader();
		System.out.println(loader.getClass());

		// 2 目标对象实现的接口

		Class<?>[] interfaces = target.getClass().getInterfaces();

		for (Class<?> face : interfaces) {
			System.out.println(face.getClass().getName());
		}

		// 3 事件处理器

		InvocationHandler h = new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				
				//拿到方法的名字 和 接口
				
				String add = method.getName();
				
				System.out.println("add="+add);
				
				
				//拿到方法所实现的接口  com.zsrt.scott.dao.StudentDao
				
				String namespace = method.getDeclaringClass().getName();
				
				System.out.println("namespace="+namespace);
				
				//去扫描所有的mapper namespace  然后再去找所有的节点  找id= add
				
				
				//占位符
				
				//insert into oa_student_t(NAME,AGE) VALUES(#{name},#{age})
				//insert into oa_student_t(NAME,AGE) VALUES("yanminghao",17)
				String sql=generateSQLByStudent(args[0]);
				
				//insert into oa_student_t(ID,NAME,AGE) VALUES( "66667", yanminghao7, 177 )
				System.out.println(sql);
				
				
				//执行sql
				
				Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/oa", "root", "123456");
				
				
				PreparedStatement ps = connection.prepareStatement(sql);
				
				ps.executeUpdate();
				
				
				
				
				Object jieguo = method.invoke(target, args);
				
				
				return jieguo;
			}
		};
		
		Object object = Proxy.newProxyInstance(loader, interfaces, h);
		System.err.println("object=" + object.getClass().getName());
		return object;
	}
	
	
	public String generateSQLByStudent(Object student){
		String sql ="insert into oa_student_t(ID,NAME,AGE) VALUES(#{id},#{name},#{age})";
		
		/*int begin = sql.indexOf("#{");
		
		System.out.println(begin);
		
		
		String temp = sql.substring(begin+2);
		
		System.out.println(temp);
		
		int end = temp.indexOf("}");
		
		System.out.println(end);
		
		String pa= temp.substring(0, 4);
		System.out.println(pa);*/
		
		
		//#{
		
		
		String[] sqls = sql.split("\\#\\{");
		
		System.out.println(sqls.length);
		
		
		System.out.println("循环前------");
		
		for(int index=0; index<=sqls.length-1;index++){
			
			Object sl= sqls[index];
			
			System.out.println(sl);
			
			
		}

		
		//4 所有只需要循环3次
		System.out.println("循环后------------------");
		for(int index=1; index<=sqls.length-1;index++){
			
			String sl= (String)sqls[index];
			
			//属性的名称
			String fname = sl.substring(0, sl.length()-2);
			
			System.out.println(fname);
			String fangfa = "get"+fname.substring(0, 1).toUpperCase()+fname.substring(1);
			
			// id   getId()
			
			//拿到id属性之后  要遍历对象 student 的所有方法  找到  getId 方法  并且把调用后的值 放到原来的地方
			
			
			Class  clazz = student.getClass();
			
			
			Method[] ms = clazz.getDeclaredMethods();
			
			for(Method method: ms){
				
				String name = method.getName();
				
				
				//根据属性的名称得到方法的名称
				
				
				
				//System.out.println("name="+name + ",fangfa="+fangfa);
				
				
				if(name.equals(fangfa)){
					
					Object object=null;
					try {
						object = method.invoke(student, null);
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
					
					
					sqls[index]=(String)object;
				}
				
			}
			
			
			
		}
		
		
		
		System.out.println("循环后------");
		
		for(int index=0; index<=sqls.length-1;index++){
			
			Object sl= sqls[index];
			
			System.out.println(sl);
			
			
		}
		
		
		//根据结果形成最终的sql
		
		StringBuffer sb = new StringBuffer();
		
		
		sb.append(sqls[0]).append(" ");
		
		
		for(int index=1;index<=sqls.length-2;index++){
			String value =sqls[index];
			
			sb.append("'"+value+"'").append(", ");
		}
		
		sb.append("'"+sqls[3]+"'" + " )");
		
		System.out.println("sql= "+ sb.toString());
		
		return sb.toString();
	}

}
